
%% calibrate level parameters

param_alpha=ones(2,2);
param_psi_m=ones(2,2);
param_psi_f=ones(2,2);

options = optimset('MaxIter',10000,'Display','iter','PlotFcns',@optimplotfval);

x0=log([param_alpha(1,1:2) param_psi_m(1,1:2) param_alpha(2,1:2) param_psi_m(2,1:2) param_psi_f(2,1:2)]);

[x,fval,exitflag,output]=fminunc(@distance,x0,options);

y=distance(x);

%% save calibrated parameters
x=exp(x);

param_alpha(1,1:2)=x(1:2);    
param_psi_m(1,1:2)=x(3:4);

param_alpha(2,1:2)=x(5:6);    
param_psi_m(2,1:2)=x(7:8);
param_psi_f(2,1:2)=x(9:10);

clear tab
tab(1,:)=param_alpha(1,:);
tab(2,:)=param_psi_m(1,:);
tab(3,:)=param_alpha(2,:);
tab(4,:)=param_psi_m(2,:);
tab(5,:)=param_psi_f(2,:);
csvwrite('../temp/level_param.csv',tab)

%% save targets and fit
clear tab
tab(1,:)=level_model(1,:,1);
tab(2,:)=level_model(1,:,2);
tab(3,:)=level_model(2,:,1);
tab(4,:)=level_model(2,:,2);
tab(5,:)=level_model(2,:,3);
csvwrite('../temp/level_model.csv',tab)

clear tab
tab(1,:)=share_model(1,:,1);
tab(2,:)=share_model(1,:,3);
tab(3,:)=share_model(2,:,1);
tab(4,:)=share_model(2,:,2);
tab(5,:)=share_model(2,:,3);
csvwrite('../temp/share_model.csv',tab)
